package com.zondy.mapgis.psmap.manager.controller;

import com.zondy.mapgis.common.controllerprefix.annotation.ManagerRestController;
import com.zondy.mapgis.common.core.utils.StringUtils;
import com.zondy.mapgis.common.core.web.controller.BaseController;
import com.zondy.mapgis.common.core.web.domain.AjaxResult;
import com.zondy.mapgis.common.log.annotation.Log;
import com.zondy.mapgis.common.log.enums.BusinessType;
import com.zondy.mapgis.common.security.utils.SecurityUtils;
import com.zondy.mapgis.domain.psmap.AppStore;
import com.zondy.mapgis.domain.psmap.AppStoreWidget;
import com.zondy.mapgis.psmap.manager.service.IAppStoreService;
import com.zondy.mapgis.psmap.manager.service.IAppStoreWidgetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Tag(
   name = "应用管理",
   description = "应用控制器"
)
@ManagerRestController({"/app/store"})
public class AppStoreController extends BaseController {
   @Autowired
   private IAppStoreService appStoreService;
   @Autowired
   private IAppStoreWidgetService appStoreWidgetService;

   @Operation(
      summary = "获取应用配置信息"
   )
   @GetMapping({"/config"})
   public AjaxResult getConfig() {
      Long userId = SecurityUtils.getUserId();
      return this.success(this.appStoreService.fillupAppStore(this.appStoreService.selectAppStoreByAppId(1L), userId));
   }

   @Operation(
      summary = "修改应用配置信息"
   )
   @Log(
      title = "应用",
      businessType = BusinessType.UPDATE
   )
   @PutMapping({"/config"})
   public AjaxResult editConfig(@RequestBody AppStore appStore) {
      appStore.setAppId(1L);
      appStore.setAppConfig(this.appStoreService.extractAppStore(appStore.getAppConfig(), SecurityUtils.getUsername()));
      appStore.setUpdateBy(SecurityUtils.getUsername());
      return this.toAjax(this.appStoreService.updateAppStore(appStore));
   }

   @Operation(
      summary = "查询应用微件列表"
   )
   @GetMapping({"/config/widget/list"})
   public AjaxResult list(AppStoreWidget appStoreWidget) {
      Long userId = SecurityUtils.getUserId();
      List<AppStoreWidget> list = this.appStoreWidgetService.selectAppStoreWidgetList(appStoreWidget, userId);
      return this.success(list);
   }

   @Operation(
      summary = "获取应用微件配置信息"
   )
   @GetMapping({"/config/widget/config"})
   public AjaxResult getAppWidgetConfig(@RequestParam(value = "appWidgetId",required = false) String appWidgetId, @RequestParam(value = "widgetName",required = false) String widgetName) {
      AppStoreWidget appStoreWidget = this.appStoreService.getAppWidget(appWidgetId, widgetName);
      if (StringUtils.isNull(appStoreWidget)) {
         if (StringUtils.isNotEmpty(appWidgetId)) {
            return this.error("应用微件ID：" + appWidgetId + "不存在");
         } else {
            return StringUtils.isNotEmpty(widgetName) ? this.error("微件名称：" + widgetName + "不存在") : this.error("微件不存在");
         }
      } else {
         return this.success(this.appStoreService.getAppWidgetConfig(appStoreWidget));
      }
   }

   @Operation(
      summary = "修改应用微件配置信息"
   )
   @Log(
      title = "应用微件",
      businessType = BusinessType.UPDATE
   )
   @PutMapping({"/config/widget/config"})
   public AjaxResult editAppWidget(@RequestBody AppStoreWidget appStoreWidget) {
      appStoreWidget.setUpdateBy(SecurityUtils.getUsername());
      return this.toAjax(this.appStoreWidgetService.updateAppStoreWidget(appStoreWidget));
   }
}
